<template>
  <el-dialog
    v-model="dialogVisible"
    :title="isEdit ? '编辑项目 ' : '创建项目'"
    width="500"
  >
    <el-form :model="AppendForm" ref="AppendFormRef" label-width="auto">
      <el-form-item
        label="项目编号"
        prop="no"
        :rules="[
          {
            required: true,
            message: '请输入项目编号',
            trigger: 'blur',
          },
        ]"
      >
        <el-input v-model="AppendForm.no" placeholder="请输入项目编号" />
      </el-form-item>
      <el-form-item
        label="项目名称"
        prop="name"
        :rules="[
          {
            required: true,
            message: '请输入项目名称',
            trigger: 'blur',
          },
        ]"
      >
        <el-input v-model="AppendForm.name" placeholder="请输入项目名称" />
      </el-form-item>
      <el-form-item label="指导教师">
        <el-select
          v-model="AppendForm.teacherId"
          filterable
          remote
          reserve-keyword
          placeholder="按 教师姓名 搜索"
          :remote-method="remoteMethod"
          :loading="selectLoading"
          clearable
        >
          <el-option
            v-for="item in options_teacherId"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="项目简介">
        <el-input
          v-model="AppendForm.proDescription"
          placeholder="请输入项目简介"
          :autosize="{ minRows: 4, maxRows: 8 }"
          type="textarea"
        />
      </el-form-item>
      <el-form-item label="项目要求">
        <el-input
          v-model="AppendForm.proRequirement"
          placeholder="请输入项目要求"
          :autosize="{ minRows: 4, maxRows: 8 }"
          type="textarea"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="onAppend(AppendFormRef)">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from "vue";
import api from "@/api";
import type { FormInstance } from "element-plus";
import { ElMessage } from "element-plus";
const emit = defineEmits(["refresh"]);

const dialogVisible = ref<boolean>(false);

const AppendFormRef = ref();
const AppendForm = ref<any>({
  no: "",
  name: "",
  teacherId: "",
  proDescription: "",
  proRequirement: "",
});

const options_teacherId = ref<any[]>([]);
const selectLoading = ref(false);
const isEdit = ref(false);

watch(dialogVisible, (bool) => {
  if (!bool) {
    AppendForm.value = {
      no: "",
      name: "",
      teacherId: "",
      proDescription: "",
      proRequirement: "",
    };
    isEdit.value = false;
  }
});

const remoteMethod = (query: string) => {
  if (query) {
    selectLoading.value = true;
    setTimeout(() => {
      api.user
        .selectTeacherByName(query)
        .then((res: any) => {
          options_teacherId.value = res.success ? res.data : [];
          selectLoading.value = false;
        })
        .catch(() => {
          selectLoading.value = false;
        });
    }, 200);
  } else {
    options_teacherId.value = [];
  }
};
function onAppend(formEl: FormInstance | undefined) {
  if (!formEl) return;
  //@ts-ignore
  formEl.validate((valid) => {
    if (valid) {
      if (!isEdit.value) { //新增
        api.project.insertCourseList(AppendForm.value).then((res: any) => {
          ElMessage({
            message: res.message,
            type: res.success ? "success" : "error",
            grouping: true,
            showClose: true,
          });
          if (res.success) {
            emit("refresh");
            dialogVisible.value = false;
          }
        });
      } else {
        api.project.updateCourseList(AppendForm.value).then((res: any) => {
          ElMessage({
            message: res.message,
            type: res.success ? "success" : "error",
            grouping: true,
            showClose: true,
          });
          if (res.success) {
            emit("refresh");
            dialogVisible.value = false;
          }
        });
      }
    }
  });
}

const open = (row: any) => {
  if (row) {
    api.project.selectCourseListByID(row.id).then((res: any) => {
      AppendForm.value = res.data;
      if(!!res?.data?.teacherName){
        remoteMethod(res.data.teacherName);
      }
      AppendForm.value = {
        id: res.data?.id,
        no: res.data?.no,
        name: res.data?.name,
        proDescription: res.data?.proDescription,
        proRequirement: res.data?.proRequirement,
      };
      if(res.data?.teacherId) AppendForm.value.teacherId = Number(res.data?.teacherId);
    });

    isEdit.value = true;
  }

  dialogVisible.value = true;
};

defineExpose({ open });
</script>
